﻿// 注释


using Admin.EcsWeb.Enum;
using Admin.EcsWeb.Service.Grpc;
using EVE.ECS_Model.Client.BCR;
using EVE.ECS_Model.Client.ECS;
using EVE.ECS_Model.Client.HK;
using EVE.ECS_Model.Client.PLC;
using EVE.ECS_Model.Client.Statistic;
using EVE.ECS_Model.Server.Config;
using EVE.ECS_Model.Server.Process;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using GeneralCommon.Models.Connect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.EcsWeb.Service;

/// <summary>
/// ClientLogService服务
/// </summary>
[ApiDescriptionSettings("ECS应用", Order = 100)]
public class GrpcServerService : IDynamicApiController, ITransient
{
   
    #region 字段
    private readonly IGrpcService _grpcService;
    #endregion

    #region 构造函数
    public GrpcServerService(IGrpcService grpcService)
    {
        _grpcService = grpcService;
    }
    #endregion

    #region 内部方法


    #region 获取数据
    /// <summary>
    /// 获取PLC数据
    /// </summary>
    /// <param name="searchPlc"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("plcgroup")]
    [ApiDescriptionSettings(Name = "plcgroup")]
    public async Task<List<PushPlcGroup>> GetPlcGroups(SearchPlcModel searchPlc)
    {
        var result = await _grpcService.GetPushGroups(searchPlc);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<PushPlcGroup>();
        }
    }

    /// <summary>
    /// 获取Bcr数据
    /// </summary>
    /// <param name="searchPlc"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("bcrgroup")]
    [ApiDescriptionSettings(Name = "bcrgroup")]
    public async Task<List<PushBcrGroup>> GetBcrGroups(SearchPlcModel searchPlc)
    {
        var result = await _grpcService.GetBcrGroups(searchPlc);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<PushBcrGroup>();
        }
    }

    /// <summary>
    /// 获取Hk数据
    /// </summary>
    /// <param name="searchPlc"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("hkgroup")]
    [ApiDescriptionSettings(Name = "hkgroup")]
    public async Task<List<PushHKGroup>> GetHkGroups(SearchPlcModel searchPlc)
    {
        var result = await _grpcService.GetHkGroups(searchPlc);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<PushHKGroup>();
        }
    }



    /// <summary>
    /// 获取设备的UnitTags数据
    /// </summary>
    /// <param name="searchUnit"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("unittag")]
    [ApiDescriptionSettings(Name = "unittag")]
    public async Task<List<ECSUnitTag>> GetUnitTags(SearchUnitModel searchUnit)
    {
        var result = await _grpcService.GetUnitTags(searchUnit);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<ECSUnitTag>();
        }
    }

    /// <summary>
    /// 获取UnitCv)(输送线）数据
    /// </summary>
    /// <param name="searchUnit"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("unitcv")]
    [ApiDescriptionSettings(Name = "unitcv")]
    public async Task<List<EcsCvUnit>> GetCvUnits(SearchUnitModel searchUnit)
    {
        var result = await _grpcService.GetCvUnits(searchUnit);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<EcsCvUnit>();
        }
    }

    /// <summary>
    /// 获取UnitSc)(堆垛机）数据
    /// </summary>
    /// <param name="searchUnit"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("unitsc")]
    [ApiDescriptionSettings(Name = "unitsc")]
    public async Task<List<EcsScUnit>> GetScUnits(SearchUnitModel searchUnit)
    {
        var result = await _grpcService.GetScUnits(searchUnit);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            return new List<EcsScUnit>();
        }
    }
    #endregion

    /// <summary>
    /// 获取注册的客户端配置
    /// </summary>
    /// <param name="id"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("connect")]
    [ApiDescriptionSettings(Name = "connect")]
    [AllowAnonymous]
    public async Task<SignClient> ConnectAsync(int id, string name)
    {
        var result = await _grpcService.ConnectAsync(id, name);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(ErrorGrpcEnum.G0002);
        }
    }



    #region 写入数据

    /// <summary>
    /// 控制扫码枪读取托盘
    /// </summary>
    /// <param name="searchPlc"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("bcrread")]
    [ApiDescriptionSettings(Name = "bcrread")]
    public async Task SendBcrRead(SearchPlcModel searchPlc)
    {
        var result = await _grpcService.SendBcrRead(searchPlc);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }




    /// <summary>
    /// 控制输送线写入数据
    /// </summary>
    /// <param name="write"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("cvwrite")]
    [ApiDescriptionSettings(Name = "cvwrite")]
    public async Task SendCvWrite(PlcCVAddr write)
    {
        var result = await _grpcService.SendCvWrite(new List<PlcCVAddr> { write });
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }


    /// <summary>
    /// 控制堆垛机写入数据
    /// </summary>
    /// <param name="write"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("scwrite")]
    [ApiDescriptionSettings(Name = "scwrite")]
    public async Task SendScWrite(PlcSCAddr write)
    {
        var result = await _grpcService.SendScWrite(new List<PlcSCAddr> { write });
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 杭可设备写入数据
    /// </summary>
    /// <param name="write"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("hkwrite")]
    [ApiDescriptionSettings(Name = "hkwrite")]
    public async Task SendHkWrite(HkWriteInfo write)
    {
        var result = await _grpcService.SendHkWrite(write);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }
    /// <summary>
    /// 获取设备统计信息（包含异常）
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("groupstatistic")]
    [ApiDescriptionSettings(Name = "groupstatistic")]
    public async Task<List<GroupRunningData>> GetGroupStatistic()
    {
        var result = await _grpcService.GetGroupStatistic();
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    #endregion


    #region 出入库任务
    /// <summary>
    /// 获取入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("enterjob")]
    [ApiDescriptionSettings(Name = "enterjob")]
    public async Task<List<ScEnterJob>> GetEnterJob()
    {
        var result = await _grpcService.GetEnterJobs(0);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 获取移库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("movejob")]
    [ApiDescriptionSettings(Name = "movejob")]
    public async Task<List<ScMoveJob>> GetMoveJob()
    {
        var result = await _grpcService.GetMoveJobs(0);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 获取充放电入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("appointjob")]
    [ApiDescriptionSettings(Name = "appointjob")]

    public async Task<List<HkWriteInfo>> GetAppoints()
    {
        var result = await _grpcService.GetFmtAppoints();
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 移除充放电入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("removeappoint")]
    [ApiDescriptionSettings(Name = "removeappoint")]
    public async Task RemoveAppoint(HkWriteInfo write)
    {
        var result = await _grpcService.RemoveFmtAppint(write);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 获取出库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("outjob")]
    [ApiDescriptionSettings(Name = "outjob")]
    public async Task<List<ScOutJob>> GetOutJob()
    {
        var result = await _grpcService.GetOutJobs(0);
        if (result.IsSuccess)
        {
            return result.Data;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 移除入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("deleteinjob")]
    [ApiDescriptionSettings(Name = "deleteinjob")]
    public async Task RemoveEnterJob(ScEnterJob scNum)
    {
        var result = await _grpcService.RemoveEnterJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 移除入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("deletemovejob")]
    [ApiDescriptionSettings(Name = "deletemovejob")]
    public async Task RemoveMoveJob(ScMoveJob scNum)
    {
        var result = await _grpcService.RemoveMoveJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 移除入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("completemovejob")]
    [ApiDescriptionSettings(Name = "completemovejob")]
    public async Task CompleteMoveJob(ScMoveJob scNum)
    {
        var result = await _grpcService.CompleteMoveJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 移除出库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("deleteoutjob")]
    [ApiDescriptionSettings(Name = "deleteoutjob")]
    public async Task RemoveOutJob(ScOutJob scNum)
    {
        var result = await _grpcService.RemoveOutJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 完成出库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("completeoutjob")]
    [ApiDescriptionSettings(Name = "completeoutjob")]
    public async Task CompleteOutJob(ScOutJob scNum)
    {
        var result = await _grpcService.CompleteOutJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }

    /// <summary>
    /// 完成入库任务
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("completeinjob")]
    [ApiDescriptionSettings(Name = "completeinjob")]
    public async Task CompleteEnterJob(ScEnterJob scNum)
    {
        var result = await _grpcService.CompleteEnterJob(scNum);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }
    #endregion

    #endregion

    #region Grpc接口方法
    /// <summary>
    /// 控制设备
    /// </summary>
    /// <param name="scNum"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("controlequip")]
    [ApiDescriptionSettings(Name = "controlequip")]
    public async Task ControlEquip(ConnectGroupData groupData)
    {
        var result = await _grpcService.ControlEquip(groupData);
        if (result.IsSuccess)
        {
            return;
        }
        else
        {
            throw Oops.Oh(result.Msg);
        }
    }
    #endregion

}

      
   

